<?php
class QPoHeader
{	
	public function __construct($p_strCountry)
	{
		$this->_country	= $p_strCountry;
		$this->_table	= $this->_country.'_poheaders';
	}
	
	public function getbyId($id)
	{
		$db	= Zend_Registry::get('db');
		$strQuery	= 'SELECT * FROM '. $this->_table . ' WHERE id=' . intval($id) . ';';
		$resultValue = $db->fetchAll($strQuery);
		$db->closeConnection();
		return $resultValue;
	}
	
	public function getbySupplierId($supplier_id)
	{
		$db	= Zend_Registry::get('db');
		$strQuery	= 'SELECT * FROM '. $this->_table . ' WHERE supplier_id=' . intval($supplier_id) . ';';
		$resultValue = $db->fetchAll($strQuery);
		$db->closeConnection();
		return $resultValue;
	}
	
	public function getlastPONumber()
	{
		$db	= Zend_Registry::get('db');
		$strQuery	= 'SELECT * FROM '. $this->_table . ' order by po_number desc limit 0, 1;';
		$resultValue = $db->fetchAll($strQuery);
		$db->closeConnection();
		return $resultValue;
	}

	public function getAll()
	{
		$db	= Zend_Registry::get('db');
		$strQuery	= 'SELECT * FROM '. $this->_table . ' Order by supplier_id;';
		$resultValue = $db->fetchAll($strQuery);
		$db->closeConnection();
		return $resultValue;
	}
	
	public function deletebyId($id)
	{
		$db	= Zend_Registry::get('db');
		$strQuery	= 'DELETE FROM '. $this->_table . ' WHERE id=' . intval($id) . ';';
		$db->execute($strQuery);
		$db->closeConnection();
	}
	
	public function insert($p_arrParams)
	{
		$db	= Zend_Registry::get('db');
		$resultValue =$db->insert($this->_table , $p_arrParams);
		$db->closeConnection();
		return $resultValue;
	}
	
	public function update($p_arrParams)
	{
		$db	= Zend_Registry::get('db');
		$resultValue = $db->update($this->_table, $p_arrParams, 'id=' .intval($p_arrParams["id"]));
		$db->closeConnection();
		return $resultValue;
	}
	
	public function truncate()
	{
		$db	= Zend_Registry::get('db');
		$resultValue = $db->execute('TRUNCATE TABLE ' . $this->_table . ';');
		$db->closeConnection();
		return $resultValue;
	}
	
	public function clone_table($date, $p_intAdminId)
	{
		$strTable	= $this->_country.'_poheader_'. $date;
		$strQuery	= '
			CREATE TABLE '.$strTable.' LIKE '.$this->_table.';
			INSERT '.$strTable.' SELECT * FROM '.$this->_table.';
			INSERT INTO '.$this->_country.'_po_archive_logs(iname, createdate, admin_id) VALUES("'.$strTable.'", sysdate(), "'.$p_intAdminId.'");
		';
		$db			= Zend_Registry::get('db');
	
	}
	
	public function insert_Archives()
	{
		$strQuery	= 'Delete from '. $this->_table . '_archives where DATE_FORMAT(created_at, "%Y-%m-%d")  =
		DATE_FORMAT(now(), "%Y-%m-%d") ;

		insert into '. $this->_table .'_archives 
		(id, supplier_id, total_sku, po_number, total_amount, logistic_type, 
		logistic_method, logistic_date, logistic_eta, created_at)
		select *, now() as created_at from ' . $this->_table . ' ; ';
		
		$db			= Zend_Registry::get('db');
		$db->execute($strQuery);
		$db->closeConnection();
	}
	
	public function getCostTaxItemAll($arrSupplierId)
	{ 	
		$strQuery	= 'select p.sku, sp.cost,sp.tax_amount, sup.bob_id_supplier from ims_supplier_product sp
						inner join ims_product p on p.id_product = fk_product
						inner join ims_supplier sup on sp.fk_supplier = sup.id_supplier
						where  sup.bob_id_supplier IN' . ';';
						 	
		
	}
	
}
?>